﻿using System;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PD.Web.DynamicData.FieldTemplates
{
    /// <summary>
    /// Represents the date time field template control.
    /// </summary>
    public partial class DateTime_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        /// <summary>
        /// The default date attribute.
        /// </summary>
        private static DataTypeAttribute defaultDateAttribute = new DataTypeAttribute(DataType.Date);

        /// <summary>
        /// Gets the data control that handles the data field in a field template.
        /// </summary>
        /// <returns>A data control that handles the data field in a field template.</returns>
        public override Control DataControl
        {
            get
            {
                return this.DateTxt;
            }
        }

        /// <summary>
        /// Gets the string representation of the value of a column in the current row when the row is in edit mode.
        /// </summary>
        /// <returns>The value of the column in the row that is edited. </returns>
        public override string FieldValueEditString
        {
            get
            {
                DateTime dt;
                if (DateTime.TryParse(base.FieldValueEditString, out dt))
                {
                    this.Calendar.SelectedDate = dt;
                    return dt.ToShortDateString();
                }

                return base.FieldValueEditString;
            }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DateTxt.ToolTip = Column.Description;

            this.SetUpValidator(this.RequiredFieldValidator1);
            this.SetUpValidator(this.RegularExpressionValidator1);
            this.SetUpValidator(this.DynamicValidator1);
            this.SetUpCustomValidator(this.DateValidator);
        }

        /// <summary>
        /// Handles the ServerValidate event of the DateValidator control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="args">The <see cref="System.Web.UI.WebControls.ServerValidateEventArgs"/> instance containing the event data.</param>
        protected void DateValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            DateTime dummyResult;
            args.IsValid = DateTime.TryParse(args.Value, out dummyResult);
        }

        /// <summary>
        /// Provides dictionary access to all data in the current row.
        /// </summary>
        /// <param name="dictionary">The dictionary that contains all the new values.</param>
        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = this.ConvertEditedValue(this.DateTxt.Text);
        }

        /// <summary>
        /// Sets up custom validator.
        /// </summary>
        /// <param name="validator">The validator.</param>
        private void SetUpCustomValidator(CustomValidator validator)
        {
            if (this.Column.DataTypeAttribute != null)
            {
                switch (this.Column.DataTypeAttribute.DataType)
                {
                    case DataType.Date:
                    case DataType.DateTime:
                    case DataType.Time:
                        validator.Enabled = true;
                        this.DateValidator.ErrorMessage = HttpUtility.HtmlEncode(this.Column.DataTypeAttribute.FormatErrorMessage(Column.DisplayName));
                        break;
                }
            }
            else if (this.Column.ColumnType.Equals(typeof(DateTime)))
            {
                validator.Enabled = true;
                this.DateValidator.ErrorMessage = HttpUtility.HtmlEncode(defaultDateAttribute.FormatErrorMessage(Column.DisplayName));
            }
        }
    }
}
